﻿using BaseUI;
using BLL;
using System;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
using Thksoft.Module.DataDetail;

namespace Thksoft.UserCotrols
{
    public partial class PlanDetailTwo : BaseControl
    {
        public DataTable dt;

        protected void Page_Load(object sender, EventArgs e)
        {
            BindData();
        }

        //天然气购入周明细表(T2)
        public void BindData()
        {
            var pPage = this.Page as ProjectFundDetail;

            lblyear.Text = pPage.myDate;
            lbldw.Text = pPage.cmyName;
            if (pPage.dtYear.Rows.Count > 0)
            {
                FORM_KEY1.Value = pPage.dtYear.Rows[0]["N_YEAR"].ToString();
                FORM_KEY2.Value = pPage.dtYear.Rows[0]["N_MONTH"].ToString();
                FORM_KEY3.Value = pPage.dtYear.Rows[0]["N_WEEK"].ToString();
            }

            dt = new QS_PLAN_DETAIL2_BLL().GetTable((int)pPage.planappId, 3);

            var row = dt.NewRow();
            row["C_CUSTOMER_NAME"] = "合计";
            row["N_USEAMOUNT"] = dt.AsEnumerable().Sum(a => (a["N_USEAMOUNT"] == DBNull.Value ? 0 : Convert.ToDecimal(a["N_USEAMOUNT"])));
            dt.Rows.Add(row);

            ViewState["database"] = dt;
            grvShow2.DataSource = dt;
            grvShow2.DataBind();
        }

        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvShow1_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            string SortOrder, OrderDire;
            SortOrder = ViewState["ORDER"] == null ? null : (string)ViewState["ORDER"];
            OrderDire = ViewState["ORDER_TYPE"] == null ? null : (string)ViewState["ORDER_TYPE"];
            if (SortOrder == sPage)
            {
                if (OrderDire == "DESC")
                    OrderDire = "ASC";
                else
                    OrderDire = "DESC";
            }
            else
            {
                SortOrder = e.SortExpression;
                OrderDire = "ASC";
            }
            ViewState["ORDER"] = SortOrder;
            ViewState["ORDER_TYPE"] = OrderDire;
        }

        public void BindDaodu()
        {
            //var dt = (DataTable)ViewState["database"];
            //dt.Columns["C_CUSTOMER_NAME"].ColumnName = "单位名称";
            //dt.Columns["C_USER_TYPE"].ColumnName = "用户类别";
            //dt.Columns["N_DOSAGE"].ColumnName = "预计用气量";
            //dt.Columns["N_PRICE"].ColumnName = "单价";
            //dt.Columns["N_AMOUNT"].ColumnName = "预计气款金额";
            //dt.Columns["C_BANK"].ColumnName = "银行现金流向";
            //dt.Columns["C_REMARK"].ColumnName = "备注";
            //dt.Columns.Remove("N_ID2");
            //dt.Columns.Remove("N_PRO_ID");
            //dt.Columns.Remove("N_TYPE");
            //dt.Columns.Remove("N_PLANAPP_ID");
            //dt.Columns.Remove("N_CUSTOMER_ID");
            //dt.Columns.Remove("N_NUM");
            //dt.Columns.Remove("r");

            var execll = new BasePage();
            //execll.toExecl1(dt, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "天然气购入周明细表");
            execll.toExecl1(Panel1, "天然气购入周明细表" + DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.Millisecond.ToString());
        }

        public void BindPrint()
        {
            var key = Guid.NewGuid().ToString();
            Session[key] = FORM_PRINT_VALUE.Value;
            MessageBox.ResponseScript(Page, "top.MessageBox.Print({Value:\"ID=HTML&VALUE=" + key + "\",Area:[\"800px\",\"500px\"],Scrollbar:true})");
        }
    }
}